package com.data.center.controller;

import com.data.center.dto.OrganizationDto;
import com.data.center.entity.Organization;
import com.data.center.exception.ResultBody;
import com.data.center.exception.exception.BizException;
import com.data.center.exception.info.CommonEnum;
import com.data.center.mapper.OrganizationMapper;
import com.data.center.service.OrganizationService;
import com.data.center.util.ExcelToListUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.List;

@SpringBootTest
@Slf4j
class neoOrganizationControllerTestDS {

    @Autowired
//    @MockBean(name = "MongoOrganizationController")
    OrganizationController organizationController;

    @Autowired
    OrganizationMapper organizationMapper;
    @Autowired
    OrganizationService organizationService;

    @Test
    void importData() {
        addOrganizationList1();
        testAddOrganizationList2();
        testAddOrganizationList2s1();
        testAddOrganizationList2s2();
    }

    @Test
    void addOrganizationList1() {
        // 该文件中香港、澳门地区组织使用其区名，无法检索到，返回添加失败提示消息供业务方自行检查
        String url = "C:\\Users\\zjjxt\\Desktop\\科协\\workspace\\data-center\\source\\OrgImportData\\OrgTemplate.xlsx";
        List<OrganizationDto> organizationDtos = ExcelToListUtils.mapExcelToObjects(url);
        log.info("添加结果" + organizationController.addOrganization(organizationDtos));
        // 输入2770条组织信息，
        // 共成功添加2756条组织信息，
        // 失败14条（有一条为吉利学院区域信息为空无法导入，12条为港澳台地区高校由于地区名称未整理无法导入，有一条为香港中文大学（深圳）括号重名无法导入）
        // 入库机制在高校数据中表现良好，对于库中不存在的区域编码和名称提供了较为完整的校验，对仅输入区域名称的数据也可以达到正确的映射
    }

    @Test
    void testAddOrganizationList2() {
        String url = "C:\\Users\\zjjxt\\Desktop\\科协\\workspace\\data-center\\source\\OrgImportData\\testAddOrganizationList2.xlsx";
        List<OrganizationDto> organizationDtos = ExcelToListUtils.mapExcelToObjects(url);
        log.info("添加结果" + organizationController.addOrganization(organizationDtos));
        // 第一批的14条数据整理后，删除了一条香港中文大学（深圳）的数据、修正了吉利学院的错误地区信息
        // 重新导入后全部成功
    }

    @Test
    void testAddOrganizationList2s1() {
        String url = "C:\\Users\\zjjxt\\Desktop\\科协\\workspace\\data-center\\source\\OrgImportData\\科协组织导入数据.xlsx";
        List<OrganizationDto> organizationDtos = ExcelToListUtils.mapExcelToObjects(url);
        log.info("添加结果" + organizationController.addOrganization(organizationDtos));
        // 科协数据初次导入
    }

    @Test
    void testAddOrganizationList2s2() {
        String url = "C:\\Users\\zjjxt\\Desktop\\科协\\workspace\\data-center\\source\\OrgImportData\\科协数据第二次导入数据.xlsx";
        List<OrganizationDto> organizationDtos = ExcelToListUtils.mapExcelToObjects(url);
        log.info("添加结果" + organizationController.addOrganization(organizationDtos));
        // 科协数据第二次导入
    }


    @Test
    void testUpdateOrganization() {
        // 1171104020882919424	民办合肥财经职业学院		02	4134014058			CHN	04340100			1
        OrganizationDto organizationDto = organizationMapper.findOrganization("西安交通工程学院", "").get(0);
        log.info("初始组织信息:" + organizationDto);

        OrganizationDto organization = new OrganizationDto();
        organization.setOrganizationId(organizationDto.getOrganizationId());

        organization.setCategory("未知");
        organization.setCountry("中国");
        organization.setUscc("testtesttest");
        organization.setDivisionCode("赤峰市");

//        MongoOrganizationController.updateOrganization(organization);
        log.info("未添加增量信息的更新方法结果:" + organizationController.updateOrganization(organization));

        organization.getAdditionalInformation().put("category", "");
        organization.getAdditionalInformation().put("country", "");
        organization.getAdditionalInformation().put("uscc", "");
        organization.getAdditionalInformation().put("divisionCode", "");
//        MongoOrganizationController.updateOrganization(organization);
        log.info("使用了增量信息的更新方法结果:" + organizationController.updateOrganization(organization));

        organization.getAdditionalInformation().put("divisionCode", "DELETE"); // 使用增量信息，删除该字段
//        MongoOrganizationController.updateOrganization(organization);
        log.info("使用了增量信息的设置某字段默认值:" + organizationController.updateOrganization(organization));

        organization.setName("民办合肥财经职业学院改");
        organization.getAdditionalInformation().put("name", "");
//        Mockito.doThrow(new BizException(CommonEnum.NOT_ALLOWED)).when(MongoOrganizationController).updateOrganization(organization);
//        log.info("使用了增量信息尝试更新名称:" + organizationMapper.findOrganization("西安交通工程学院", "").get(0));

    }
}